"use strict"
//充值统计
const P = require('bluebird')
const mvc = require("cosjs.mvc");

exports.page = function () {
    let uid = this.get('uid', 'string')
    let ids = uid.split(',')
    let data = {
        "page": 1,
        "size": 1,
        "total": 1,
        "records": 10,
        "rows": []
    };
    let task = []
    let te = new Date()
    te.setHours(0, 0, 0, 0)
    te = te.getTime()
    let ts = te - 24 * 3600 * 1000
    for (let i = 1; i < 9; i++) {
        let model = mvc.model.mongo('order', i)
        task.push(P.all([
            model.aggregate([
                { $match: { time: { $gte: te }, uid: { $nin: ids } } },
                { $group: { _id: null, sum: { $sum: '$val' } } }
            ], {}),
            model.aggregate([
                { $match: { time: { $gte: ts, $lte: te }, uid: { $nin: ids } } },
                { $group: { _id: null, sum: { $sum: '$val' } } }
            ], {}),
            model.aggregate([
                { $match: { time: { $lte: te }, uid: { $nin: ids } } },
                { $group: { _id: null, sum: { $sum: '$val' } } }
            ], {}),
        ]))
    }
    return P.all(task)
        .then(ret => {
            let t1 = 0
            let t2 = 0
            let t3 = 0
            ret.forEach((item, i) => {
                let v1 = item[0]['null'] ? item[0]['null'].sum : 0
                let v2 = item[1]['null'] ? item[1]['null'].sum : 0
                let v3 = item[2]['null'] ? item[2]['null'].sum : 0
                t1 += v1
                t2 += v2
                t3 += v3
                data.rows.push({
                    "server": i + 1,
                    "today": v1,
                    "yesterday": v2,
                    "total": v3
                })
            })
            data.rows.push({
                "server": 'total',
                "today": t1,
                "yesterday": t2,
                "total": t3
            })
            return data
        })
}


